﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TweetSharp;

namespace Twitter_Data_Crawler
{
    public partial class frmPhanTich : Form
    {
        private string mstrUserID;
        private int mintSelectIndex;

        public frmPhanTich()
        {
            InitializeComponent();
        }

        private void frmPhanTich_Load(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                //Load user data
                dgvUser.DataSource = xLoadUser();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private DataTable xLoadUser()
        {
            try
            {
                string strSQL = "";
                DataTable objData = null;

                strSQL = "Select tUser.*, tFeature.* From tUser Left Join tFeature On tUser.UserID = tFeature.UserID";

                return objData = Program.gobjDb.fncReadData(strSQL);
            }
            catch { return null; }
        }

        private DataTable xLoadUserTweet(string strUserID)
        {
            try
            {
                string strSQL = "";
                DataTable objData = null;

                strSQL = "Select * From tTweet Where UserID = '" + strUserID + "'";

                return objData = Program.gobjDb.fncReadData(strSQL);
            }
            catch { return null; }
        }

        private void dgvUser_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvUser.CurrentRow.Index < 0) return;

                Cursor.Current = Cursors.WaitCursor;
                //load user detail

                int row = dgvUser.CurrentRow.Index;
                string strUserID;
                string strName;
                string strScreenName;
                string strFollowing;
                string strFollower;
                string strSQL = "";
                bool blnStatus;
                DataTable objData;
                DataTable objTweet;

                //doc du lieu tu grid
                strUserID = clsCommon.fncCnvNullToString(dgvUser.Rows[row].Cells[clmUserID.Name].Value);
                strName = clsCommon.fncCnvNullToString(dgvUser.Rows[row].Cells[clmName.Name].Value);
                strScreenName = clsCommon.fncCnvNullToString(dgvUser.Rows[row].Cells[clmScreenName.Name].Value);
                strFollowing = clsCommon.fncCnvNullToString(dgvUser.Rows[row].Cells[clmFollowing.Name].Value);
                strFollower = clsCommon.fncCnvNullToString(dgvUser.Rows[row].Cells[clmFollower.Name].Value);
                mstrUserID = strUserID;
                mintSelectIndex = row;

                //fill textbox
                txtName.Text = strName;
                txtScreenName.Text = strScreenName;
                txtFollowing.Text = strFollowing;
                txtFollower.Text = strFollower;

                //txtFoF.Text = clsCommon.fncCnvNullToString(dgvUser.Rows[row].Cells[clmFollower.Name].Value);
                //txtSimilarity.Text = clsCommon.fncCnvNullToString(dgvUser.Rows[row].Cells[clmFollower.Name].Value);
                //txtUrlCount.Text = clsCommon.fncCnvNullToString(dgvUser.Rows[row].Cells[clmFollower.Name].Value);
                //txtMentionCount.Text = clsCommon.fncCnvNullToString(dgvUser.Rows[row].Cells[clmFollower.Name].Value);
                //txtHashTag.Text = clsCommon.fncCnvNullToString(dgvUser.Rows[row].Cells[clmFollower.Name].Value);

                //txtIndexFoF.Text = clsCommon.fncCnvNullToString(dgvUser.Rows[row].Cells[clmFollower.Name].Value);
                //txtIndexUrl.Text = clsCommon.fncCnvNullToString(dgvUser.Rows[row].Cells[clmFollower.Name].Value);
                //txtIndexMention.Text = clsCommon.fncCnvNullToString(dgvUser.Rows[row].Cells[clmFollower.Name].Value);
                //txtIndexHashtag.Text = clsCommon.fncCnvNullToString(dgvUser.Rows[row].Cells[clmFollower.Name].Value);
                //txtIndexSimilar.Text = clsCommon.fncCnvNullToString(dgvUser.Rows[row].Cells[clmFollower.Name].Value);

                //fill tweets
                objTweet = xLoadUserTweet(strUserID);
                dgvContent.DataSource = objTweet;
                grpTweet.Text = "Tweets gần đây";
                if (objTweet != null) grpTweet.Text = "Tweets gần đây - " + objTweet.Rows.Count;
                btnGoto_Click(null, null);

                //load danh gia
                strSQL += " Select";
                strSQL += " [UserID]";
                strSQL += ",[FoF]";
                strSQL += ",[Url]";
                strSQL += ",[Similar]";
                strSQL += ",[Mention]";
                strSQL += ",[HashTag]";
                strSQL += ",[Retweet]";
                strSQL += ",[FsubF]";
                strSQL += ",[Index_FoF]";
                strSQL += ",[Index_Url]";
                strSQL += ",[Index_Similar]";
                strSQL += ",[Index_Mention]";
                strSQL += ",[Index_HashTag]";
                strSQL += ",[Index_Retweet]";
                strSQL += ",[Index_FsubF]";
                strSQL += ",[Conclude]";

                strSQL += " From tFeature";
                strSQL += " Where UserID = " + strUserID;

                objData = Program.gobjDb.fncReadData(strSQL);

                if (objData == null)
                {
                    //ko co du lieu
                    txtStatus.Text = "Chưa đánh giá";
                    txtStatus.ForeColor = Color.Black;
                    txtStatus.BackColor = Color.White;
                }
                else
                {
                    //da dc danh gia
                    txtFoF.Text = clsCommon.fncCnvNullToString(objData.Rows[0]["FoF"]);
                    txtUrlCount.Text = clsCommon.fncCnvNullToString(objData.Rows[0]["Url"]);
                    txtSimilarity.Text = clsCommon.fncCnvNullToString(objData.Rows[0]["Similar"]);
                    txtMentionCount.Text = clsCommon.fncCnvNullToString(objData.Rows[0]["Mention"]);
                    txtHashTag.Text = clsCommon.fncCnvNullToString(objData.Rows[0]["HashTag"]);
                    txtRetweet.Text = clsCommon.fncCnvNullToString(objData.Rows[0]["Retweet"]);
                    txtFsubF.Text = clsCommon.fncCnvNullToString(objData.Rows[0]["FsubF"]);

                    txtIndexFoF.Text = clsCommon.fncCnvNullToString(objData.Rows[0]["Index_FoF"]);
                    txtIndexUrl.Text = clsCommon.fncCnvNullToString(objData.Rows[0]["Index_Url"]);
                    txtIndexSimilar.Text = clsCommon.fncCnvNullToString(objData.Rows[0]["Index_Similar"]);
                    txtIndexMention.Text = clsCommon.fncCnvNullToString(objData.Rows[0]["Index_Mention"]);
                    txtIndexHashtag.Text = clsCommon.fncCnvNullToString(objData.Rows[0]["Index_HashTag"]);
                    txtIndexRetweet.Text = clsCommon.fncCnvNullToString(objData.Rows[0]["Index_Retweet"]);
                    txtFsubF.Text = clsCommon.fncCnvNullToString(objData.Rows[0]["Index_FsubF"]);

                    string strStatusTemp = clsCommon.fncCnvNullToString(objData.Rows[0]["CONCLUDE"]);
                    if (clsCommon.fncIsBlank(strStatusTemp))
                    {
                        txtStatus.Text = "Chưa đánh giá";
                        txtStatus.BackColor = Color.White;
                        return;
                    }

                    blnStatus = clsCommon.fncCnvNullToBool(strStatusTemp);
                    if (blnStatus)
                    {
                        txtStatus.Text = "SPAM";
                        txtStatus.BackColor = Color.Red;
                    }
                    else
                    {
                        txtStatus.Text = "GOOD";
                        txtStatus.BackColor = Color.Lime;
                    }
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void btnGoto_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                //go to twitter
                objBrowser.Url = new Uri(@"https://mobile.twitter.com/" + txtScreenName.Text);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private int xCountAttr(List<string> lstTweet, clsEnum.CountAttr emAttr)
        {
            const string cstrURL = @"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?";
            const string cstrMention = @"@([^@ ]+)";
            const string cstrHashtag = @"#([^@ ]+)";

            int intResult = 0;
            int intTemp = 0;

            try
            {
                if (lstTweet == null) return 0;

                System.Text.RegularExpressions.Regex objRegex;
                string strPattern = "";
                string strTemp;

                switch (emAttr)
                {
                    case clsEnum.CountAttr.URL: strPattern = cstrURL; break;
                    case clsEnum.CountAttr.MENTION: strPattern = cstrMention; break;
                    case clsEnum.CountAttr.HASHTAG: strPattern = cstrHashtag; break;
                }

                objRegex = new System.Text.RegularExpressions.Regex(strPattern);

                for (int i = 0; i <= lstTweet.Count - 1; i++)
                {
                    strTemp = lstTweet[i].Replace(",", " ");
                    intTemp = objRegex.Matches(strTemp).Count;
                    if (emAttr == clsEnum.CountAttr.MENTION && intTemp > 0)
                    {
                        //neu bat dau la retweet: RT @username thi ko tinh
                        if (strTemp.StartsWith("RT ")) intTemp--;
                    }

                    intResult += intTemp;
                }

                return intResult;
            }
            catch { return 0; }
        }

        private int xCountSimilarTweet(List<string> lstTweet)
        {
            const string cstrURL = @"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?";
            const string cstrMention = @"@([^@ ]+)";
            const string cstrHashtag = @"#([^@ ]+)";

            int intResult = 0;

            try
            {
                if (lstTweet == null) return 0;

                int intDistance;
                List<string> lstTemp;
                Random objRandom = new Random();

                lstTemp = new List<string>();

                //copy list
                for (int i = 0; i <= lstTweet.Count - 1; i++)
                {
                    lstTemp.Add(lstTweet[i]);
                }

                //xoa het cac url, mention, hashtag    
                for (int i = 0; i <= lstTemp.Count - 1; i++)
                {
                    lstTemp[i] = System.Text.RegularExpressions.Regex.Replace(lstTemp[i], cstrURL, "");
                    lstTemp[i] = System.Text.RegularExpressions.Regex.Replace(lstTemp[i], cstrMention, "");
                    lstTemp[i] = System.Text.RegularExpressions.Regex.Replace(lstTemp[i], cstrHashtag, "");
                    lstTemp[i] = lstTemp[i].Trim();
                    if (lstTemp[i].ToLower() == "tôi đã đăng tải ảnh mới lên facebook" || lstTemp[i].ToLower() == "i posted a new photo to facebook")
                        lstTemp[i] += objRandom.NextDouble().ToString();

                    if (clsCommon.fncIsBlank(lstTemp[i])) lstTemp[i] += objRandom.NextDouble().ToString();
                }

                int intTempResult = 0;

                //kiem tra xem co giong nhau ko.
                //vong lap chay tu cuoi len dau
                for (int i = lstTemp.Count - 1; i >= 1; i--)
                {
                    intTempResult = 0;

                    for (int j = i - 1; j >= 0; j--)
                    {
                        intDistance = clsCommon.fncLevenshteinDistance2(lstTemp[i], lstTemp[j]);

                        //neu 2 string gan giong nhau
                        if (intDistance <= 5)
                        {
                            //tang bien dem
                            if (intTempResult == 0) { intTempResult += 2; }
                            else { intTempResult++; }

                            //xoa ket qua vua trung lap
                            lstTemp.RemoveAt(j);

                            i--;
                        }
                    }

                    intResult += intTempResult;

                    //xoa phan tu dang duyet
                    lstTemp.RemoveAt(i);

                    //khoi tao lai bien dem
                    i = lstTemp.Count;
                }

                return intResult;
            }
            catch { return 0; }
        }

        private int xCountRetweet(List<string> lstTweet)
        {
            int intResult = 0;

            try
            {
                if (lstTweet == null) return 0;

                string strTemp;

                for (int i = 0; i <= lstTweet.Count - 1; i++)
                {
                    strTemp = lstTweet[i].Replace(",", " ");
                    strTemp = strTemp.Trim();
                    if (strTemp.StartsWith("RT ")) intResult++;
                }

                return intResult;
            }
            catch { return 0; }
        }

        private void btnSPAM_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                xUpdateStatus(mstrUserID, clsEnum.Conclude.SPAM);
                txtStatus.BackColor = Color.Red;

                ////move next row
                //if (dgvUser.CurrentRow.Index + 1 > dgvUser.Rows.Count) return;
                //dgvUser.Rows[dgvUser.CurrentRow.Index].Selected = false;
                //dgvUser.Rows[dgvUser.CurrentRow.Index + 1].Selected = true;
                //dgvUser_Click(null, null);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void btnGOOD_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                xUpdateStatus(mstrUserID, clsEnum.Conclude.GOOD);
                txtStatus.BackColor = Color.Lime;

                ////move next row
                //if (dgvUser.CurrentRow.Index + 1 > dgvUser.Rows.Count) return;
                //dgvUser.Rows[dgvUser.CurrentRow.Index].Selected = false;
                //dgvUser.Rows[dgvUser.CurrentRow.Index + 1].Selected = true;
                //dgvUser_Click(null, null);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private bool xUpdateStatus(string strUserID, clsEnum.Conclude emStt)
        {
            try
            {
                string strSQl = "";
                strSQl += " Update tFeature";
                strSQl += " Set";

                if (emStt == clsEnum.Conclude.SPAM)
                    strSQl += " Conclude = 'true'";
                else
                    strSQl += " Conclude = 'false'";

                strSQl += " Where";
                strSQl += " UserID = " + strUserID;

                if (!Program.gobjDb.fncExcuteSQL(strSQl)) return false;

                if (emStt == clsEnum.Conclude.SPAM)
                {
                    txtStatus.Text = "SPAM";
                    txtStatus.BackColor = Color.Red;
                    dgvUser.Rows[mintSelectIndex].DefaultCellStyle.BackColor = Color.Red;
                }
                else
                {
                    txtStatus.Text = "GOOD";
                    txtStatus.BackColor = Color.Lime;
                    dgvUser.Rows[mintSelectIndex].DefaultCellStyle.BackColor = Color.Lime;
                }

                return true;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return false; }
        }

        private bool xAnalysis(string strUserID, int intFollowing, int intFollower)
        {
            try
            {
                double dblFoF;
                int intUrlCount, intSameTweet, intMentionCount, intHashtagCount, intRetweet, intFsubF;
                DataTable objTweet;
                List<string> lstTweet;
                string strTemp;

                DataTable objDataTemp;

                //load tweets
                objTweet = xLoadUserTweet(strUserID);
                lstTweet = new List<string>();

                if (objTweet == null) return false;

                for (int i = 0; i <= objTweet.Rows.Count - 1; i++)
                {
                    strTemp = clsCommon.fncCnvNullToString(objTweet.Rows[i]["Content"]);
                    if (clsCommon.fncIsBlank(strTemp)) continue;
                    lstTweet.Add(strTemp);
                }

                if (clsCommon.fncCnvNullToDbl(intFollower) == 0.0) dblFoF = 9999999;
                else dblFoF = clsCommon.fncCnvNullToDbl(clsCommon.fncCnvNullToDbl(intFollowing) / (clsCommon.fncCnvNullToDbl(intFollower) + clsCommon.fncCnvNullToDbl(intFollowing)));
                intUrlCount = xCountAttr(lstTweet, clsEnum.CountAttr.URL);
                intMentionCount = xCountAttr(lstTweet, clsEnum.CountAttr.MENTION);
                intHashtagCount = xCountAttr(lstTweet, clsEnum.CountAttr.HASHTAG);
                intSameTweet = xCountSimilarTweet(lstTweet);
                intRetweet = xCountRetweet(lstTweet);
                intFsubF = intFollowing - intFollower;

                //txtFoF.Text = string.Format("{0:0.00}", dblFoF);
                //txtSimilarity.Text = intSameTweet.ToString();
                //txtUrlCount.Text = intUrlCount.ToString();
                //txtMentionCount.Text = intMentionCount.ToString();
                //txtHashTag.Text = intHashtagCount.ToString();

                //txtIndexFoF.Text = string.Format("{0:0.00}", dblFoF);
                //txtIndexUrl.Text = string.Format("{0:0.00}", (double)intUrlCount / (double)lstTweet.Count);
                //txtIndexMention.Text = string.Format("{0:0.00}", (double)intMentionCount / (double)lstTweet.Count);
                //txtIndexHashtag.Text = string.Format("{0:0.00}", (double)intHashtagCount / (double)lstTweet.Count);
                //txtIndexSimilar.Text = string.Format("{0:0.00}", (double)intSameTweet / (double)lstTweet.Count);

                //update db
                string strSQl = "";

                //▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
                //strSQl += " Delete From tFeature Where UserID = " + strUserID + ";";

                //strSQl += " Insert Into tFeature";
                //strSQl += " (";
                //strSQl += " [UserID]";

                //strSQl += ",[FoF]";
                //strSQl += ",[Url]";
                //strSQl += ",[Similar]";
                //strSQl += ",[Mention]";
                //strSQl += ",[HashTag]";

                //strSQl += ",[Index_FoF]";
                //strSQl += ",[Index_Url]";
                //strSQl += ",[Index_Similar]";
                //strSQl += ",[Index_Mention]";
                //strSQl += ",[Index_HashTag]";
                //strSQl += " )";
                //strSQl += " Values";
                //strSQl += " (";
                //strSQl += " '" + strUserID + "'";

                //strSQl += ",'" + string.Format("{0:0.00}", dblFoF) + "'";
                //strSQl += ",'" + intUrlCount + "'";
                //strSQl += ",'" + intSameTweet + "'";
                //strSQl += ",'" + intMentionCount + "'";
                //strSQl += ",'" + intHashtagCount + "'";

                //strSQl += ",'" + string.Format("{0:0.00}", dblFoF) + "'";
                //strSQl += ",'" + string.Format("{0:0.00}", (double)intUrlCount / (double)lstTweet.Count) + "'";
                //strSQl += ",'" + string.Format("{0:0.00}", (double)intSameTweet / (double)lstTweet.Count) + "'";
                //strSQl += ",'" + string.Format("{0:0.00}", (double)intMentionCount / (double)lstTweet.Count) + "'";
                //strSQl += ",'" + string.Format("{0:0.00}", (double)intHashtagCount / (double)lstTweet.Count) + "'";

                //strSQl += " )";


                strSQl += " SELECT * From tFeature Where UserID = " + strUserID;
                objDataTemp = Program.gobjDb.fncReadData(strSQl);
                strSQl = "";

                if (objDataTemp != null)
                {
                    strSQl += " UPDATE";
                    strSQl += " tFeature";
                    strSQl += " SET";

                    strSQl += " [FoF] = '" + string.Format("{0:0.00}", dblFoF) + "'";
                    strSQl += ",[Url] = '" + intUrlCount + "'";
                    strSQl += ",[Similar] = '" + intSameTweet + "'";
                    strSQl += ",[Mention] = '" + intMentionCount + "'";
                    strSQl += ",[HashTag] = '" + intHashtagCount + "'";
                    strSQl += ",[Retweet] = '" + intRetweet + "'";
                    strSQl += ",[FsubF] = '" + intFsubF + "'";

                    strSQl += ",[Index_FoF] = '" + string.Format("{0:0.00}", dblFoF) + "'";
                    strSQl += ",[Index_Url] = '" + string.Format("{0:0.00}", (double)intUrlCount / (double)lstTweet.Count) + "'";
                    strSQl += ",[Index_Similar] = '" + string.Format("{0:0.00}", (double)intSameTweet / (double)lstTweet.Count) + "'";
                    strSQl += ",[Index_Mention] = '" + string.Format("{0:0.00}", (double)intMentionCount / (double)lstTweet.Count) + "'";
                    strSQl += ",[Index_HashTag] = '" + string.Format("{0:0.00}", (double)intHashtagCount / (double)lstTweet.Count) + "'";
                    strSQl += ",[Index_Retweet] = '" + string.Format("{0:0.00}", (double)intRetweet / (double)lstTweet.Count) + "'";
                    strSQl += ",[Index_FsubF] = '" + intFsubF.ToString() + "'";

                    strSQl += " WHERE UserID = " + strUserID;
                }
                else
                {
                    strSQl += " Insert Into tFeature";
                    strSQl += " (";
                    strSQl += " [UserID]";

                    strSQl += ",[FoF]";
                    strSQl += ",[Url]";
                    strSQl += ",[Similar]";
                    strSQl += ",[Mention]";
                    strSQl += ",[HashTag]";
                    strSQl += ",[Retweet]";
                    strSQl += ",[FsubF]";

                    strSQl += ",[Index_FoF]";
                    strSQl += ",[Index_Url]";
                    strSQl += ",[Index_Similar]";
                    strSQl += ",[Index_Mention]";
                    strSQl += ",[Index_HashTag]";
                    strSQl += ",[Index_Retweet]";
                    strSQl += ",[Index_FsubF]";
                    strSQl += " )";
                    strSQl += " Values";
                    strSQl += " (";
                    strSQl += " '" + strUserID + "'";

                    strSQl += ",'" + string.Format("{0:0.00}", dblFoF) + "'";
                    strSQl += ",'" + intUrlCount + "'";
                    strSQl += ",'" + intSameTweet + "'";
                    strSQl += ",'" + intMentionCount + "'";
                    strSQl += ",'" + intHashtagCount + "'";
                    strSQl += ",'" + intRetweet + "'";
                    strSQl += ",'" + intFsubF + "'";

                    strSQl += ",'" + string.Format("{0:0.00}", dblFoF) + "'";
                    strSQl += ",'" + string.Format("{0:0.00}", (double)intUrlCount / (double)lstTweet.Count) + "'";
                    strSQl += ",'" + string.Format("{0:0.00}", (double)intSameTweet / (double)lstTweet.Count) + "'";
                    strSQl += ",'" + string.Format("{0:0.00}", (double)intMentionCount / (double)lstTweet.Count) + "'";
                    strSQl += ",'" + string.Format("{0:0.00}", (double)intHashtagCount / (double)lstTweet.Count) + "'";
                    strSQl += ",'" + string.Format("{0:0.00}", (double)intRetweet / (double)lstTweet.Count) + "'";
                    strSQl += ",'" + intFsubF.ToString() + "'";

                    strSQl += " )";
                }
                //▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲

                Program.gobjDb.fncExcuteSQL(strSQl);

                return true;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return false; }
        }

        private void mniPhanTich_Click(object sender, EventArgs e)
        {
            try
            {
                string strUserID;
                int intFollowing;
                int intFollower;

                Cursor.Current = Cursors.WaitCursor;

                for (int i = 0; i <= dgvUser.Rows.Count - 1; i++)
                {
                    DataGridViewCheckBoxCell objCheck = (DataGridViewCheckBoxCell)dgvUser.Rows[i].Cells[clmSelect.Index];
                    if (!clsCommon.fncCnvNullToBool(objCheck.Value)) continue;

                    strUserID = clsCommon.fncCnvNullToString(dgvUser.Rows[i].Cells[clmUserID.Name].Value);
                    intFollowing = clsCommon.fncCnvNullToInt(dgvUser.Rows[i].Cells[clmFollowing.Name].Value);
                    intFollower = clsCommon.fncCnvNullToInt(dgvUser.Rows[i].Cells[clmFollower.Name].Value);

                    xAnalysis(strUserID, intFollowing, intFollower);
                }

                MessageBox.Show("Done!");
            }
            catch { }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void dgvUser_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            try
            {
                string strTemp;
                bool blnSpam;

                for (int i = 0; i <= dgvUser.Rows.Count - 1; i++)
                {
                    dgvUser.Rows[i].Cells[clmSTT.Name].Value = (i + 1).ToString();

                    strTemp = clsCommon.fncCnvNullToString(dgvUser.Rows[i].Cells["Conclude"].Value);
                    if (clsCommon.fncIsBlank(strTemp)) continue;

                    blnSpam = clsCommon.fncCnvNullToBool(strTemp);
                    if (blnSpam) { dgvUser.Rows[i].DefaultCellStyle.BackColor = Color.Red; }
                    else { dgvUser.Rows[i].DefaultCellStyle.BackColor = Color.Lime; }
                }
            }
            catch { }
        }

        private void dgvUser_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex < 0) return;
                if (e.ColumnIndex != clmSelect.Index) return;

                DataGridViewCheckBoxCell objCheck = (DataGridViewCheckBoxCell)dgvUser.Rows[e.RowIndex].Cells[e.ColumnIndex];

                if (clsCommon.fncCnvNullToBool(objCheck.Value))
                {
                    objCheck.Value = false;
                }
                else
                {
                    objCheck.Value = true;
                }
            }
            catch { }
        }

        private void mniChonTatCa_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i <= dgvUser.Rows.Count - 1; i++)
                {
                    DataGridViewCheckBoxCell objCheck = (DataGridViewCheckBoxCell)dgvUser.Rows[i].Cells[clmSelect.Index];

                    objCheck.Value = true;

                }
            }
            catch { }
        }

        private void mniBoChonTatCa_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i <= dgvUser.Rows.Count - 1; i++)
                {
                    DataGridViewCheckBoxCell objCheck = (DataGridViewCheckBoxCell)dgvUser.Rows[i].Cells[clmSelect.Index];

                    objCheck.Value = false;

                }
            }
            catch { }
        }

        private void mniXoa_Click(object sender, EventArgs e)
        {
            try
            {
                string strUserID;
                string strSQL;
                bool blnSuccess = true;

                Cursor.Current = Cursors.WaitCursor;

                Program.gobjDb.fncBeginTransaction();

                for (int i = 0; i <= dgvUser.Rows.Count - 1; i++)
                {
                    DataGridViewCheckBoxCell objCheck = (DataGridViewCheckBoxCell)dgvUser.Rows[i].Cells[clmSelect.Index];
                    if (!clsCommon.fncCnvNullToBool(objCheck.Value)) continue;

                    strUserID = clsCommon.fncCnvNullToString(dgvUser.Rows[i].Cells[clmUserID.Name].Value);

                    if (!blnSuccess) continue;
                    strSQL = "Delete From tFeature Where UserID = " + strUserID;
                    blnSuccess = blnSuccess && Program.gobjDb.fncExcuteSQL(strSQL, true);

                    if (!blnSuccess) continue;
                    strSQL = "Delete From tTweet Where UserID = " + strUserID;
                    blnSuccess = blnSuccess && Program.gobjDb.fncExcuteSQL(strSQL, true);

                    if (!blnSuccess) continue;
                    strSQL = "Delete From tUser Where UserID = " + strUserID;
                    blnSuccess = blnSuccess && Program.gobjDb.fncExcuteSQL(strSQL, true);
                }

                if (blnSuccess)
                {
                    Program.gobjDb.fncCommit();
                    MessageBox.Show("Success!");
                }
                else
                {
                    Program.gobjDb.fncRollback();
                    MessageBox.Show("Delete Fail!");
                }

            }
            catch { }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void mniGetNewData_Click(object sender, EventArgs e)
        {
            try
            {
                string strUserID;
                int intFollowing;
                int intFollower;

                Cursor.Current = Cursors.WaitCursor;

                TwitterService objTwitter = new TwitterService("IMIEGHoUBuziJ9b6Beg", "WAd3SB98Vs0B6fsRib2hOvLRN0NEJ3hLYUPcYBjFo");
                objTwitter.AuthenticateWith("230944527-t0uu72szETPuuk09PjDqP34TYSOABAS975CDk4Ha", "gmFfRyztyREYdNBcZFy0ALfpqDgJKogU06jh7BS231YpF");

                ListTweetsOnUserTimelineOptions objUserTweetOption = new ListTweetsOnUserTimelineOptions();
                objUserTweetOption.Count = 100;

                StringBuilder strBuilder = new StringBuilder();
                long lngUserID = 0;
                string mstrSQL = "";
                string strTemp = "";

                for (int i = 0; i <= dgvUser.Rows.Count - 1; i++)
                {
                    DataGridViewCheckBoxCell objCheck = (DataGridViewCheckBoxCell)dgvUser.Rows[i].Cells[clmSelect.Index];
                    if (!clsCommon.fncCnvNullToBool(objCheck.Value)) continue;

                    strUserID = clsCommon.fncCnvNullToString(dgvUser.Rows[i].Cells[clmUserID.Name].Value);
                    intFollowing = clsCommon.fncCnvNullToInt(dgvUser.Rows[i].Cells[clmFollowing.Name].Value);
                    intFollower = clsCommon.fncCnvNullToInt(dgvUser.Rows[i].Cells[clmFollower.Name].Value);

                    long.TryParse(strUserID, out lngUserID);
                    objUserTweetOption.UserId = lngUserID;
                    objUserTweetOption.ScreenName = clsCommon.fncCnvNullToString(dgvUser.Rows[i].Cells[clmScreenName.Name].Value);
                    objUserTweetOption.ExcludeReplies = true;
                    //objUserTweetOption.Count = 20;
                    //objUserTweetOption.IncludeRts = false;
                    IEnumerable<TwitterStatus> objUsetTweets = null;
                    try
                    {
                        objUsetTweets = objTwitter.ListTweetsOnUserTimeline(objUserTweetOption);
                    }
                    catch { continue; }

                    if (objUsetTweets != null)
                    {
                        mstrSQL += " Delete From tTweet Where [UserID]= " + lngUserID + ";";

                        foreach (TwitterStatus objTwitStt in objUsetTweets)
                        {
                            strTemp = "";
                            try
                            {
                                strTemp += " Insert Into  tTweet";
                                strTemp += "(";
                                strTemp += " [UserID]";
                                strTemp += ",[TweetID]";
                                strTemp += ",[Content]";
                                strTemp += ")";

                                strTemp += " Values";
                                strTemp += "(";
                                strTemp += " '" + lngUserID + "'";
                                strTemp += ",'" + objTwitStt.Id + "'";
                                strTemp += ",'" + clsCommon.fncCnvToDbString(objTwitStt.TextDecoded) + "'";
                                strTemp += ")";
                                strTemp += ";";
                            }
                            catch { continue; }
                            mstrSQL += strTemp;
                        }
                    }
                }

                bool blnSuccess = true;

                //Program.gobjDb.fncBeginTransaction();

                blnSuccess = blnSuccess && Program.gobjDb.fncExcuteSQL(mstrSQL, false);

                if (blnSuccess)
                {
                    Program.gobjDb.fncCommit();
                    //xLoadData();
                    MessageBox.Show("Success!");
                }
                else
                {
                    //Program.gobjDb.fncRollback();
                    MessageBox.Show("Fail...");
                }

                MessageBox.Show("Done!");
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

    }
}
